<?php
class ModelMenuMenu extends Model {
	public function getMenu($menu_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id) WHERE m.restaurant_id = '" . (int)$this->config->get('config_restaurant_id') . "' AND m.menu_id = '" . (int)$menu_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "' AND m.status = '1'");
		
		return $query->row;
	}
	
	public function getMenus($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "menu m LEFT JOIN " . DB_PREFIX . "menu_description md ON (m.menu_id = md.menu_id) WHERE m.restaurant_id = '" . (int)$this->config->get('config_restaurant_id') . "' AND m.parent_id = '" . (int)$parent_id . "' AND md.language_id = '" . (int)$this->config->get('config_language_id') . "' AND m.status = '1' ORDER BY m.sort_order, LCASE(md.name)");
		
		return $query->rows;
	}

	public function getMenusByParentId($menu_id) {
		$menu_data = array();
		
		$menu_query = $this->db->query("SELECT menu_id FROM " . DB_PREFIX . "menu WHERE parent_id = '" . (int)$menu_id . "'");
		
		foreach ($menu_query->rows as $menu) {
			$menu_data[] = $menu['menu_id'];
			
			$children = $this->getMenusByParentId($menu['menu_id']);
			
			if ($children) {
				$menu_data = array_merge($children, $menu_data);
			}			
		}
		
		return $menu_data;
	}
					
	public function getTotalMenusByMenuId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "menu m WHERE m.restaurant_id = '" . (int)$this->config->get('config_restaurant_id') . "' AND m.parent_id = '" . (int)$parent_id . "' AND m.status = '1'");
		
		return $query->row['total'];
	}
}
?>